//
// Copyright (c) 2024 BPer
//
#ifndef __BPerAudioDecoderHandle__
#define __BPerAudioDecoderHandle__

#include <bperutil/sys/BPerThread2.h>
#include <bperutil/buffer/BPerAudioEncoderBuffer.h>
#include <bperutil/buffer/BPerAudioPlayBuffer.h>
#include <bperdecoder/BPerAudioDecoder.h>

#include <vector>

using namespace std;
class BPerAudioDecoderHandle:public BPerThread,public BPerDecoderCallback,public BPerMediaConfigCallback
{
public:
	BPerAudioDecoderHandle(BPerContext *pcontext);
	~BPerAudioDecoderHandle(void);
	int32_t m_isStart;
private:
	int32_t m_isInit;

public:

	BPerAudioPlayBuffer* getOutAudioBuffer();
	void setRemoteParam(BPerAudioParam* para);
	void init();
	void stop();
	void setInAudioBuffer(BPerAudioEncoderBuffer *pbuf);
	void setOutAudioBuffer(BPerAudioPlayBuffer* pbuf);
	void removeAudioStream(int32_t puid);
	void removeAllStream();

	void onAudioData(BPerFrame* pframe);
	void onVideoData(BPerFrame* pframe);
	void setMediaConfig(int32_t puid, BPerAudioParam *audio,BPerVideoParam *video);

protected:
	void run();
	void stopLoop();
	void startLoop();

void saveFile(char *fileName, uint8_t *pBuffer, long BufferLen);

private:
	BPerAudioPlayBuffer* m_out_audioBuffer;
	BPerAudioDecoder *m_decs;
	int32_t m_isConvert;
	BPerAudioEncoderBuffer *m_in_audioBuffer;
	BPerContext *m_context;
	BPerAudioParam* m_param;


	bool m_is44100;
	uint8_t* m_buf;
	int32_t m_size;
	BPerFrame m_audioFrame;
	void decode(int32_t isIframe,uint8_t*src,int32_t  p_buflen,uint8_t *dest,int32_t *p_destLen);

};
#endif
